package com.wzh.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.store.entity.Address;
import com.wzh.store.mapper.AddressMapper;
import com.wzh.store.service.IAddressService;
import com.wzh.store.service.ex.*;
import com.wzh.store.utils.VoUtils;
import com.wzh.store.vo.AddressVo;
import com.wzh.store.vo.Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author wzh
 * @create 2022/4/22 17:09
 * @Description：新增收货地址接口实现类
 */
@Service
public class AddressServiceimpl extends ServiceImpl<AddressMapper,Address> implements IAddressService {
    @Autowired
    public AddressMapper addressMapper;
    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Override
    public List<Address> getByUid(Integer uid) {
        List<Address> list = addressMapper.findByUid(uid);
        for (Address address : list) {
//            address.setAid(null);
//            address.setUid(null);
            address.setProvinceCode(null);
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setTel(null);
            address.setIsDefault(null);
            address.setCreatedTime(null);
            address.setCreatedUser(null);
            address.setModifiedTime(null);
            address.setModifiedUser(null);
        }
        return list;
    }

    @Override
    @Transactional //事务管理
    public void setDefault(Integer aid, Integer uid, String username) {
        Address byAid = addressMapper.findByAid(aid);
        if (byAid==null){
            throw new AddressNotFoundException("用户地址不存在");
        }
//        检测当前数据的归属
        if (!byAid.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
//        将所有地址设置为非默认
        Integer integer = addressMapper.updateNonDefault(uid);
        if (integer==0){
            throw new UpdateException("用户更新时产生异常");
        }

        Integer integer1 = addressMapper.updateDefaultByAid(aid, username, new Date());
        if (integer1!=1){
            throw new UpdateException("用户更新时产生异常");
        }
    }

    @Override
    public void delete(Integer aid, Integer uid, String username) {
        Address byAid = addressMapper.findByAid(aid);
        int x = byAid.getIsDefault();
        if (byAid==null){
            throw new AddressNotFoundException("改地址不存在");
        }
        if (!byAid.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        addressMapper.deleteByAid(aid);

        Integer count = addressMapper.countByUId(uid);
        if (count==0){
            return;
        }
        if (x==1) {
            Address address = addressMapper.findLastModified(uid);
            Integer row = addressMapper.updateDefaultByAid(address.getAid(), username, new Date());
            if (row!=1){
                throw new UpdateException("更新时产生未知的异常");
            }

        }

    }

    @Override
    public void addNewAddress(Integer uid,String username,Address address) {
        Integer count = addressMapper.countByUId(uid);
        if (count>maxCount){
            throw new AddressCountLimitException("用户地址数量超过限定数量");
        }
        address.setUid(uid);
        Integer isDefault=count==0?1:0;//1表示默认，0表示不默认
//        补全四项日志
        address.setCreatedUser(username);
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());
        address.setCreatedTime(new Date());
        int row = addressMapper.insert(address);
        if (row!=1){
            throw new InsertException("插入用户的收货地址时产生了未知的异常");
        }
    }
}
